﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.PointCloud.CApPointCloudLayerNameGenerator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension.ModelObjects;
using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.PointCloud;

internal class CApPointCloudLayerNameGenerator
{
  private string m_layerPrefix;

  internal CApPointCloudLayerNameGenerator()
  {
    this.m_layerPrefix = PointCloudStringTable.POINT_CLOUD_LAYER_NAME_PREFIX;
  }

  public string GenerateNewLayerName(ITxPointCloud ipointCloud)
  {
    int num = this.GetIndexFromLayerName(this.GetLastLayerName(ipointCloud));
    if (num < 0)
      num = 0;
    return this.m_layerPrefix + (num + 1).ToString();
  }

  private string GetLastLayerName(ITxPointCloud ipointCloud)
  {
    string lastLayerName = string.Empty;
    TxObjectList<ITxPointCloudLayer> allLayers = TxPointCloudEx.GetAllLayers(ipointCloud);
    if (allLayers != null)
    {
      int count = ((Collection<ITxPointCloudLayer>) allLayers).Count;
      if (count > 0)
        lastLayerName = ((ITxObject) ((Collection<ITxPointCloudLayer>) allLayers)[count - 1]).Name;
    }
    return lastLayerName;
  }

  private int GetIndexFromLayerName(string layerName)
  {
    int indexFromLayerName = -1;
    if (layerName.StartsWith(this.m_layerPrefix))
    {
      int length1 = this.m_layerPrefix.Length;
      int length2 = layerName.Length - length1;
      string s = layerName.Substring(length1, length2);
      try
      {
        indexFromLayerName = int.Parse(s);
      }
      catch (Exception ex)
      {
        indexFromLayerName = -1;
      }
    }
    return indexFromLayerName;
  }
}
